<template>
    <view class="he-page-content">
        <!--<image class="he-background__box" :src="ipAddress + '/login-background-login.png'"></image>-->
        <view class="he-background__box he-content flex flex-direction align-center">
            <!--<image class="he-icon" :src="storeSetting.logo" mode="aspectFit"></image>-->
            <view class="he-text">请授权微信登录</view>
            <view class="he-other__text"> 以便我们为您提供更好的服务</view>
            <template v-if="!userInfo">
                <button
                        v-if="canIUseGetUserProfile"
                        class="cu-btn he-login"
                        @click="login"
                >
                    微信一键授权登录
                </button>
                <button
                        v-else
                        class="cu-btn he-login"
                        open-type="getUserInfo"
                        @getuserinfo="login"
                >
                    微信一键授权登录
                </button>
            </template>

            <template v-else>
                <button v-if="!userInfo.mobile"
                        class="cu-btn phone"
                        open-type="getPhoneNumber"
                        @getphonenumber="getPhoneNumber"
                >
                    绑定手机号
                </button>
            </template>
            <button class="cu-btn he-out" @click="notLogin">暂不登录</button>
        </view>
    </view>
</template>

<script>
    export default {
        name: "login",
        components: { },
        data() {
            return {
                userInfo: uni.getStorageSync('userInfo'),
            };
        },
        methods: {
            notLogin: function () {
                uni.navigateBack({ delta: 2 });
            },
            login: function () {
                if (this.canIUseGetUserProfile) {
                    this.getUserProfile().then(() => {
                        uni.navigateBack({
                            delta: 1,
                        });
                    });
                }
            },
            getUserProfile(){
                let that = this;
                return new Promise((resolve, reject) => {
                    uni.getUserProfile({
                        lang: "zh_CN",
                        desc: '授权',
                        success: function (res) {
                            uni.login({
                                provider: 'weixin',
                                success: function (loginRes) {
                                    //fixme 邀请活动
                                    let invite_code = uni.getStorageSync('invite_code');
                                    let data = {
                                        code: loginRes.code,
                                        avatarUrl: res.userInfo.avatarUrl,
                                        gender: res.userInfo.gender,
                                        nickname: res.userInfo.nickName
                                    };
                                    if (invite_code){
                                        data.invite_code = invite_code;
                                    }
                                    uni.request({
                                        url: 'https://sy-k.cn/api.php/login/wx_login', //仅为示例，并非真实接口地址。
                                        data: data,
                                        success: (resp) => {
                                            uni.setStorageSync('u_id', resp.data.u_id);
                                            if ( resp.data.aged.toString() === '1'){  // 助力过之后需要把key删除
                                                uni.removeStorage({key:'invite_code'});

                                                uni.showToast({
                                                    title: '助力成功', //提示文字
                                                    duration: 3000, //显示时长
                                                    icon: "success", //图标，支持"success"、"loading"
                                                });
                                            }

                                            uni.$u.http.post('/api.php/Landlord/user_info', {
                                                u_id:resp.data.u_id
                                            }).then(response => {
                                                uni.setStorageSync('userInfo', response);
                                                that.userInfo = response;
                                                resolve();
                                            }).catch(error => {
                                                console.error(error);
                                                reject();
                                            });
                                        }
                                    });
                                }
                            });
                        }
                    });
                });
            },

            getPhoneNumber: function(e) {
                if (!e.detail.code){
                    uni.$u.toast('未能获取到code,当前版本过低！');
                    return;
                }
                let that = this;
                uni.request({
                    url: 'https://sy-k.cn/api.php/login/get_phone', //仅为示例，并非真实接口地址。
                    method:'POST',
                    data: {
                        code: e.detail.code,
                        'u_id': uni.getStorageSync('u_id')
                    },
                    success: (resp) => {
                        if (resp.data.code === 200) {
                            uni.$u.http.post('/api.php/Landlord/user_info', {
                                u_id: uni.getStorageSync('u_id')
                            }).then(response => {
                                uni.setStorageSync('userInfo', response);
                                that.userInfo = response;
                                uni.navigateBack({
                                    delta: 1,
                                });
                            }).catch(error => {
                                console.error(error);
                            });
                        }
                    }
                });
            },
        },
        computed: {
            canIUseGetUserProfile: function () {
                return uni.canIUse("getUserProfile");
            },
        },
    };
</script>

<style scoped lang="scss">
    .he-page-content {
        background: #ffffff;
    }

    .he-background__box {
        width: 100%;
        height: 960rpx;
    }

    .he-icon {
        width: 180rpx;
        height: 180rpx;
    }

    .he-content {
        position: absolute;
        top: 0;
        padding: 160rpx 20rpx;
    }

    .cu-btn {
        width: 560rpx;
        height: 80rpx;
        border-radius: 40rpx;
        font-family: PingFang SC;
        font-weight: 500;
    }

    .he-out {
        background: #f4f4f5;
        font-size: 28rpx;
        font-weight: 500;
        color: #999999;
        margin-top: 16rpx;
    }

    .he-login {
        font-size: 30rpx;
        color: #ffffff;
        background-color: #33A7FF;
        margin-top: 88rpx;
    }

    .phone {
        font-size: 30rpx;
        color: #ffffff;
        background-color: #33A7FF;
        margin-top: 16rpx;
    }

    .he-text {
        font-size: 32rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #222222;
        margin-top: 64rpx;
    }

    .he-other__text {
        font-size: 28rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #999999;
        margin-top: 24rpx;
    }
</style>